import { createApp } from 'vue'
import App from './App.vue'

//引入封装路由
import router from './routers/index'
import rules from './routers/rules'

//引入ui框架和样式
import 'vant/lib/index.css'

////导入全部组件
import Vant from 'vant'

//引入自定义工具包
import tools from './services/tools'

//引入异步请求插件
import request from './services/request'

//引入状态管理插件
import { createPinia } from 'pinia'
//引入pinia数据持久化插件
import piniaPluginPersist from 'pinia-plugin-persist'

//引入store
import store from './store/index'
const app = createApp(App)
    .use(router)
    .use(Vant)
    .use(request)   // request -> install
    .use(createPinia().use(piniaPluginPersist))
    .use(store)
    .use(tools)
    .mount('#app')

router.beforeEach(async (to, from) => {
    if(rules.includes(to.path)) return true

    // console.log(app.$business.value)
    let {id, mobile} = app.$business.value

    var result = await app.$POST({
        url: '/business/check',
        params: {id, mobile}
    })

    if(result.code == 0)
    {
        app.$store.$reset()

        app.$fail(result.msg)

        return { path: '/business/login'}
    }

    app.$business.value = result.data

    return true
})